package org.nanyuan.forum.controller;

import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.nanyuan.forum.common.AppResult;
import org.nanyuan.forum.common.ResultCode;
import org.nanyuan.forum.exception.ApplicationException;
import org.nanyuan.forum.model.Board;
import org.nanyuan.forum.service.IBoardService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {
    @Value("${forum.index.board-num}")
    private Integer indexBoardNum;
    @Resource
    private IBoardService service;
    /*
    * 获取首页导航栏
    * */
    @PostMapping("/topList")
    public AppResult<List<Board>> topList() {
        log.info("首页板块个数为：{}", indexBoardNum);
        List<Board> boards = service.selectByNum(indexBoardNum);

        if (boards == null) {
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }
    @GetMapping("/getById")
    public AppResult<Board> getById(@RequestParam("id") @NonNull Long id) {
        //调用service
        Board board = service.selectById(id);
        //对结果进行校验
        if (board == null || board.getDeleteFlag() == 1) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //返回结果
        return AppResult.success(board);
    }
}
